package com.lzy.crm.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lzy.crm.base.mybatis.BaseMapper;
import com.lzy.crm.base.mybatis.Example;
import com.lzy.crm.base.page.PageQuery;
import com.lzy.crm.base.service.BaseServiceImpl;
import com.lzy.crm.base.util.JsonUtil;
import com.lzy.crm.base.util.StringUtil;
import com.lzy.crm.dao.CardFlowMapper;
import com.lzy.crm.dao.UserMapper;
import com.lzy.crm.dao.criteria.UserCriteria;
import com.lzy.crm.domain.CardFlow;
import com.lzy.crm.domain.User;
import com.lzy.crm.service.UserService;
import com.lzy.crm.util.UserConstants;
import com.lzy.crm.vo.AgentUser;
import com.lzy.crm.vo.CardVo;
import com.lzy.crm.vo.PlayerInfo;

@Service("userService")
public class UserServiceImpl extends BaseServiceImpl<User, UserCriteria> implements UserService {

	@Resource
	private UserMapper userMapper;
	
	@Resource
	private CardFlowMapper cardFlowMapper;
	

	@Override
	public BaseMapper<User, UserCriteria> getBaseMapper() {
		return  userMapper;
	}

	/**
	 * 查询玩家Id是否已在绑定列表中
	 * @param gameId
	 * @return
	 */
	public List<User> getUserByGameId(String gameId){
		return userMapper.getUserListByGameId(gameId);
	}
	
	@Override
	public List<User> getUserListByEmailAndPwd(String email) {
		Example<UserCriteria> example = new Example<UserCriteria>(UserCriteria.class);
		UserCriteria userCriteria = example.createCriteria();
		userCriteria.andEmailEqualTo(email);
		return userMapper.getListByExample(example);
	}

	@Override
	public List<User> getUserListByPhoneAndPwd(String phone) {
		Example<UserCriteria> example = new Example<UserCriteria>(UserCriteria.class);
		UserCriteria userCriteria = example.createCriteria();
		userCriteria.andPhoneEqualTo(phone);
		return userMapper.getListByExample(example);
	}

	
	/**
	 * 查询下级玩家
	 * @param parentId
	 * @param upParentId
	 * gameUserId
	 * @return
	 */
	@Override
	public List<User> getAllPlayerUserList(Integer parentId ,Integer upParentId ,String gameUserId,PageQuery pageQuery ){
		List<User> userList = userMapper.getAllPlayerUserList(parentId, upParentId, gameUserId,pageQuery);
		for(User user :userList){
			if(user.getUnionid()!=null){
				PlayerInfo playerInfo= getPlayerInfo(null, user.getUnionid());
				if(playerInfo!=null){
					if(user.getGameUserId()==null||user.getGameUserId().intValue()!= playerInfo.getGameId().intValue()){
						user.setGameUserId(playerInfo.getGameId());// 补上gameID
						user.setAgentId(playerInfo.getGameId());
						userMapper.updateByPrimaryKeySelective(user);
					}
					
					user.setTotalCard(playerInfo.getInsureScore());
				}
			}else{
				user.setTotalCard(0);
			}
		}
		return userList;
	}
	
	
	/**
	 * 查询玩家信息
	 * @param gameId
	 * @return
	 */
	public User getUserInfoByGameid(String gameId){
		Example<UserCriteria> example = new Example<UserCriteria>(UserCriteria.class);
		UserCriteria userCriteria = example.createCriteria();
		userCriteria.andGameUserIdEqualTo(gameId);
		List<User> userList =userMapper.getListByExample(example);
		return userList.size()>0?userList.get(0):null;
	}
	
	
	/**
	 * 查询玩家信息
	 * @param agentId
	 * @return
	 */
	public User getUserInfoByAgentId(String agentId){
		Example<UserCriteria> example = new Example<UserCriteria>(UserCriteria.class);
		UserCriteria userCriteria = example.createCriteria();
		userCriteria.andAgentIdEqualTo(agentId);
		List<User> userList =userMapper.getListByExample(example);
		return userList.size()>0?userList.get(0):null;
	}
	
	/**
	 * 根据公众号唯一标识ID 查询
	 * @param openid
	 * @return
	 */
	@Override
	public User getUserByOpenid(String openid){
		Example<UserCriteria> example = new Example<UserCriteria>(UserCriteria.class);
		UserCriteria userCriteria = example.createCriteria();
		userCriteria.andOpenidEqualTo(openid);
		return userMapper.getObjectByExample(example);
	}
	
	/**
	 * 根据开放平台唯一标识查询
	 * @param unionid
	 * @return
	 */
	@Override
	public User getUserByUnionid(String unionid){
		Example<UserCriteria> example = new Example<UserCriteria>(UserCriteria.class);
		UserCriteria userCriteria = example.createCriteria();
		userCriteria.andUnionidEqualTo(unionid);
		return userMapper.getObjectByExample(example);
	}
	
	

	@Override
	public List<AgentUser> getAgentUserList(Integer userId,String phone,String status,PageQuery pageQuery) {
		List<User> userList = userMapper.getNextUserList(userId,phone, status, pageQuery);
		List<AgentUser> agentUserList = new ArrayList<AgentUser>();
		// 组装对象
		for (User user : userList) {
			AgentUser agentUser = JsonUtil.fromJson(JsonUtil.toJson(user), AgentUser.class);
			Integer myAgentTotals = userMapper.getNextUserCount(agentUser.getId());
			agentUser.setMyAgentTotals(myAgentTotals.toString());
			/*CardVo cardVo = userMapper.getCardTot(agentUser.getId());
			if(cardVo!=null){
				agentUser.setGiftLeftNum(cardVo.getGiftLeftNum());
				agentUser.setLeftAllCard(cardVo.getLeftAllCard());
				agentUser.setSalesAllCard(cardVo.getSalesAllCard());
			}else{
				agentUser.setGiftLeftNum("0");
				agentUser.setLeftAllCard("0");
				agentUser.setSalesAllCard("0");
			}*/
			if(user.getUnionid()!=null){
				PlayerInfo playerInfo= getPlayerInfo(null, user.getUnionid());
				if(playerInfo!=null){
					if(user.getGameUserId()==null||user.getGameUserId().intValue()!= playerInfo.getGameId().intValue()){
						user.setGameUserId(playerInfo.getGameId());// 补上gameID
						user.setAgentId(playerInfo.getGameId());
						userMapper.updateByPrimaryKeySelective(user);
					}
					agentUser.setTotalCard(playerInfo.getInsureScore());
				}
			}else{
				agentUser.setTotalCard(0);
			}
			agentUserList.add(agentUser);
		}
		return agentUserList;
	}
	
	@Override
	public List<AgentUser> getAllAgentUserList(Integer userId,String phone,String status,PageQuery pageQuery) {
		List<User> userList = userMapper.getAllAgentUserList(userId,phone, status, pageQuery);
		List<AgentUser> agentUserList = new ArrayList<AgentUser>();
		// 组装对象
		for (User user : userList) {
			AgentUser agentUser = JsonUtil.fromJson(JsonUtil.toJson(user), AgentUser.class);
			Integer myAgentTotals = userMapper.getNextUserCount(agentUser.getId());
			agentUser.setMyAgentTotals(myAgentTotals.toString());
			/*CardVo cardVo = userMapper.getCardTot(agentUser.getId());
			if(cardVo!=null){
				agentUser.setGiftLeftNum(cardVo.getGiftLeftNum());
				agentUser.setLeftAllCard(cardVo.getLeftAllCard());
				agentUser.setSalesAllCard(cardVo.getSalesAllCard());
			}else{
				agentUser.setGiftLeftNum("0");
				agentUser.setLeftAllCard("0");
				agentUser.setSalesAllCard("0");
			}*/
			if(user.getUnionid()!=null){
				PlayerInfo playerInfo= getPlayerInfo(null, user.getUnionid());
				if(playerInfo!=null){
					if(user.getGameUserId()==null||user.getGameUserId().intValue()!= playerInfo.getGameId().intValue()){
						user.setGameUserId(playerInfo.getGameId());// 补上gameID
						user.setAgentId(playerInfo.getGameId());
						userMapper.updateByPrimaryKeySelective(user);
					}
					agentUser.setTotalCard(playerInfo.getInsureScore());
				}
			}else{
				user.setTotalCard(0);
			}
			agentUserList.add(agentUser);
		}
		return agentUserList;
	}

	/**
	 * 跨库查询玩家信息
	 * @param userId
	 * @param pageQuery
	 * @return
	 */
	public List<PlayerInfo> getPlayerInfoList(String gameId){
		return userMapper.getPlayerInfoList(gameId);
	}

	/**
	 * 跨库查询玩家信息
	 * @param userId
	 * @param pageQuery
	 * @return
	 */
	public PlayerInfo getPlayerInfo(Integer gameId,String unionId){
		if(gameId==null&&StringUtil.isEmpty(unionId)){
			return null; // 查询无效
		}
		return userMapper.getPlayerInfo(gameId,unionId);
	}

	
	/**
	 * 给玩家加房卡
	 * @param userId
	 * @param insureScore  饭卡数
	 */
	public void addPlayerCard(Integer userId,Integer insureScore){
		userMapper.addPlayerCard(userId, insureScore);
	}
	
	/**
	 * 给玩家赠送房卡
	 */
	public void invitedAddPlayerCard(User user,PlayerInfo parentPlayerInfo,Integer roomCardMenInvited,String invitedName)throws Exception{
		
		userMapper.addPlayerCard(parentPlayerInfo.getUserId(), roomCardMenInvited);
		// 修改卡流水  生成记录
		CardFlow cardFlow =new CardFlow();
		cardFlow.setAId(0);
		cardFlow.setAName("邀请赠送");
		cardFlow.setACard(0);
		cardFlow.setBId(user.getGameUserId());
		cardFlow.setBName(user.getUserName());
		cardFlow.setCard(roomCardMenInvited);
		cardFlow.setAwardCard(0);
		cardFlow.setBeforeCard(parentPlayerInfo.getInsureScore());
		cardFlow.setAfterCard(parentPlayerInfo.getInsureScore()+roomCardMenInvited);
		cardFlow.setFunc(0);
		cardFlow.setRemark("邀请 【"+invitedName+"】");
		cardFlow.setType(UserConstants.CARD_TYPE_INVITED);;
		cardFlow.setCreateTime(new Date());
		cardFlowMapper.insertSelective(cardFlow);
		
	}
	
	
	
	
	@Override
	public List<User> getUserListByParentId(Integer parentId){
		Example<UserCriteria> example = new Example<UserCriteria>(UserCriteria.class);
		UserCriteria userCriteria = example.createCriteria();
		userCriteria.andParentIdEqualTo(String.valueOf(parentId));
		userCriteria.andGameUserIdIsNotNull();
		return userMapper.getListByExample(example);
	}

	
	/**
	 * 清除玩家游戏问题
	 * @param gameId
	 */
	public void unlockPayerByGameId(String gameId){
		userMapper.unlockPayerByGameId(gameId);
		userMapper.unlockPlayerInRoomInfoByGameId(gameId);
	}
	
	/**
	 * 清除表
	 */
	public void unlockTable(){
		userMapper.unlockTable();
		userMapper.unlockPlayerInRoomInfo();
	}

}
